﻿
/**@brief ring buffer(kfifo)
 *@wjm  date:2014-12-18
* ring_buffer.C
 * */


#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <assert.h>

#include "ringbuffer.h"



//求比val大的最近的一个2次幂数
unsigned long roundup_power_of_two(unsigned long val)
{
	if (val & (val-1) == 0)
	{
		return val;
	}
	
	unsigned long maxulong = (unsigned long )((unsigned long ) ~0);
	unsigned long andv = ~(maxulong&(maxulong>>1));
	
	while((andv & val) == 0)
	{
		andv = andv>>1;
	}
	
	return andv<<1;
}



//初始化缓冲区
struct ring_buffer* __ring_buffer_init( struct ring_buffer *ring_buf, uint8_t *buffer, uint32_t size)
{
	assert(ring_buf  || buffer);

	assert( is_power_of_2(size) );
	
    if (!is_power_of_2(size))
    {
		fprintf(stderr,"size must be power of 2.\n");
        return ring_buf;
    }

    memset(ring_buf, 0, sizeof(struct ring_buffer));
	
    ring_buf->buffer = buffer;
    ring_buf->size = size;
    ring_buf->in = 0;
    ring_buf->out = 0;

    return ring_buf;
}

void __ring_buffer_reset(struct ring_buffer *ring_buf)
{
   ring_buf->in = 0;
   ring_buf->out = 0;
}

//缓冲区的长度
uint32_t __ring_buffer_len(const struct ring_buffer *ring_buf)
{
    return (ring_buf->in - ring_buf->out);
}

//从缓冲区中取数据
uint32_t __ring_buffer_get(struct ring_buffer *ring_buf, uint8_t * buffer, uint32_t size)
{
    assert(ring_buf || buffer);
	
    uint32_t len = 0;
    size  = min(size, ring_buf->in - ring_buf->out);        
    /* first get the data from fifo->out until the end of the buffer */
    len = min(size, ring_buf->size - (ring_buf->out & (ring_buf->size - 1) ) );
    memcpy(buffer, ring_buf->buffer + (ring_buf->out & (ring_buf->size - 1)), len);
    /* then get the rest (if any) from the beginning of the buffer */
    memcpy(buffer + len, ring_buf->buffer, size - len);
    ring_buf->out += size;
    return size;
}
//向缓冲区中存放数据
uint32_t __ring_buffer_put(struct ring_buffer *ring_buf, uint8_t *buffer, uint32_t size)
{
    assert(ring_buf || buffer);

    uint32_t len = 0;

    size = min(size, ring_buf->size - ring_buf->in + ring_buf->out);

    /* first put the data starting from fifo->in to buffer end */
    len  = min(size, ring_buf->size - (ring_buf->in & (ring_buf->size - 1)));
    memcpy(ring_buf->buffer + (ring_buf->in & (ring_buf->size - 1)), buffer, len);

    /* then put the rest (if any) at the beginning of the buffer */
    memcpy(ring_buf->buffer, buffer + len, size - len);
    ring_buf->in += size;
    return size;
}

//从buffer里面读取大小size的数据块
void * __ring_buffer_readdata(struct ring_buffer *ring_buf,  uint32_t size)
{
    assert(ring_buf );
	
	void * pOut =0;
	
    uint32_t len = __ring_buffer_len(ring_buf);
	
	if( len>size )
	{
		pOut =  ring_buf->buffer + (ring_buf->out & (ring_buf->size - 1));
		ring_buf->out += size;
	}

	return pOut;

}

//从buffer里面获取大小size的空闲buffer
void * __ring_buffer_getfreeroom(struct ring_buffer *ring_buf,  uint32_t size)
{
    assert(ring_buf );
	
	void * pOut =0;
	
    uint32_t freelen = ring_buf->size - ring_buf->in + ring_buf->out;
	
	if( freelen > size )
	{
		pOut =  ring_buf->buffer + (ring_buf->in & (ring_buf->size - 1));
		ring_buf->in += size;
	}

	return pOut;

}


#ifdef _IN_LINUX_

//初始化缓冲区
struct ring_buffer* ring_buffer_init( struct ring_buffer *ring_buf, void *buffer, uint32_t size, pthread_mutex_t *f_lock)
{
	assert(ring_buf  || buffer);

	assert( is_power_of_2(size) );
	
    if (!is_power_of_2(size))
    {
		fprintf(stderr,"size must be power of 2.\n");
        return ring_buf;
    }

    memset(ring_buf, 0, sizeof(struct ring_buffer));
	
    ring_buf->buffer = buffer;
    ring_buf->size = size;
    ring_buf->in = 0;
    ring_buf->out = 0;
	
    ring_buf->f_lock = f_lock;

    return ring_buf;
}

uint32_t ring_buffer_len(const struct ring_buffer *ring_buf)
{
    uint32_t len = 0;
    pthread_mutex_lock(ring_buf->f_lock);
    len = __ring_buffer_len(ring_buf);
    pthread_mutex_unlock(ring_buf->f_lock);
    return len;
}

uint32_t ring_buffer_get(struct ring_buffer *ring_buf, void *buffer, uint32_t size)
{
    uint32_t ret;
    pthread_mutex_lock(ring_buf->f_lock);
    ret = __ring_buffer_get(ring_buf, buffer, size);
    //buffer中没有数据
    if (ring_buf->in == ring_buf->out)
    ring_buf->in = ring_buf->out = 0;
    pthread_mutex_unlock(ring_buf->f_lock);
    return ret;
}

uint32_t ring_buffer_put(struct ring_buffer *ring_buf, void *buffer, uint32_t size)
{
    uint32_t ret;
    pthread_mutex_lock(ring_buf->f_lock);
    ret = __ring_buffer_put(ring_buf, buffer, size);
    pthread_mutex_unlock(ring_buf->f_lock);
    return ret;
}
#endif
